package g11.dao;

import g11.model.Patient;

import java.sql.Date;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class PatientDAOImpl implements PatientDAO {

	// private static final Logger log = Logger.getLogger(PatientDAOImpl.class);

	private Session session;

	public Session getSession() {
		return this.session;
	}

	public void cleanup() {
		if (session != null)
			session.close();
		// HibernateUtil.shutdown();
	}

	public PatientDAOImpl() {
		// log.info(getClass().toString());

	}

	public Patient getPatientById(int patientId) throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Patient patient;
		try {
			patient = (Patient) session.get(Patient.class, patientId);
		} catch (HibernateException e) {
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}
		return patient;
	}

	public Patient getPatientByName(String patientName) throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Patient patient = null;
		try {
			String hql = "from Patient p where p.name =?";
			Query q = session.createQuery(hql);
			q.setString(0, patientName);
			if (q.list().size() > 0) {
				patient = (Patient) q.list().get(0);
			}
		} catch (HibernateException e) {
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}
		return patient;
	}

	// need check this implementation
	public Patient getPatientByLastVisitTime(Date lastVisitTime)
			throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Patient patient = null;
		try {
			String hql = "from Patient p where p.lastVisitTime =?";
			Query q = session.createQuery(hql);
			// need to change DateTimer to Date
			// lastVisitTime.toDate();
			q.setDate(0, lastVisitTime);// q.setDate()
			if (q.list().size() > 0) {
				patient = (Patient) q.list().get(0);
			}
		} catch (HibernateException e) {
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}
		return patient;
	}

	public Patient getPatientByOhip(String ohip) throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Patient patient = null;
		try {
			String hql = "select p from Patient p where p.ohip =?";
			Query q = session.createQuery(hql);
			q.setString(0, ohip);
			if (q.list().size() > 0) {
				patient = (Patient) q.list().get(0);
			}
		} catch (HibernateException e) {
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}
		return patient;
	}

	public Integer createPatient(Patient patient) throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Transaction tran = null;
		Integer patientId;
		try {
			tran = session.beginTransaction();
			patientId = (Integer) session.save(patient);
			tran.commit();
		} catch (HibernateException e) {
			tran.rollback();
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}

		return patientId;
	}

	public void deletePatient(Patient patient) throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Transaction tran = null;
		try {
			tran = session.beginTransaction();
			session.delete(patient);
			tran.commit();
		} catch (HibernateException e) {
			tran.rollback();
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}
	}

	public void updatePatient(Patient patient) throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Transaction tran = null;
		try {
			tran = session.beginTransaction();
			session.update(patient);
			tran.commit();
		} catch (HibernateException e) {
			tran.rollback();
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}
	}

}
